package com.ly.hotel.spainterface.pp.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.ly.hotel.R;
import com.ly.hotel.common.Constants;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.common.MyLogsManager;
import com.ly.hotel.msgevent.SendLogEvent;
import com.ly.hotel.spainterface.pp.bean.PreJSDataBean;
import com.ly.hotel.util.ImageUtil;

import de.greenrobot.event.EventBus;

/**
 * Created by mo on 2018/6/4.
 */

public class PpJSDetailsDialog extends Dialog {

    private Context context;
    private ImageButton technicianDialogClose;
    private TextView technicianId;
    private TextView technicianName;
    private TextView technicianSex;
    private TextView technicianAge;
    private TextView technicianHeight;
    private TextView technicianWeight;
    private TextView technicianHome;
    private TextView technicianMemo;
    private ImageView technicianIamge;
    private Button callWaiter;
    private PreJSDataBean preJSDataBean;
    private String itemName;
    private boolean isShowCall;
    private int rod  = 0;
    private TechnicianImageDialog imageDialog;
    private final String TAG = "PpJSDetailsDialog";
    private final MyApplication app;
    private TextView callWaiterTips;
    private TextView itemType;

    public PpJSDetailsDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
        rod = context.getResources().getDimensionPixelSize(R.dimen.px6);
        app = (MyApplication) MyApplication.getContext();
    }

    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pp_js_details_layout);
        initView();
        setDialogColse();
        setJSDialogData();
        initImageDialog();
        setTechnicianImageOpen();
        setCallWaiter();
    }

    private void setCallWaiter() {
        if(isShowCall){
            if(app.isBtnItemEnable("呼叫")){// 判断是否开通呼叫功能
                String callText = "";
                callWaiter.setVisibility(View.VISIBLE);
                if(app.getCallStatus() == Constants.CALL_CANCLE){
                    callText = "立即呼叫";
                }else if(app.getCallStatus() == Constants.CALL_REQUEST){
                    callText = "取消呼叫";
                }
                callWaiter.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                app.changeLight();
                            }

                        }).start();

                        if(app.getCallStatus() == Constants.CALL_CANCLE){
                            EventBus.getDefault().post(new SendLogEvent(MyLogsManager.CLOUD_LOGS_POINT_CALL));
                        }else{
                            EventBus.getDefault().post(new SendLogEvent(MyLogsManager.CLOUD_LOGS_POINT_CANCLE_CALL));
                        }
                    }
                });
                callWaiter.setText(callText);
            }
        }else {
            callWaiter.setVisibility(View.GONE);
            callWaiterTips.setVisibility(View.VISIBLE);
        }
    }

    public void onEventMainThread(SendLogEvent sendLogEvent){
        if(app.getCallStatus() == Constants.CALL_CANCLE){
            callWaiter.setText("取消呼叫");
        }else {
            callWaiter.setText("立即呼叫");
        }
    }

    private void initImageDialog() {
        imageDialog = new TechnicianImageDialog(context, R.style.InputDialog);
        imageDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_PHONE);
        imageDialog.setImageSrc(MyApplication.getHostResUrl() + "/upload/" + preJSDataBean.getImg());
        imageDialog.setWholeUrl(true);
    }

    private void setTechnicianImageOpen() {
        technicianIamge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageDialog.show();
            }
        });
    }

    private void initView(){
        technicianDialogClose = findViewById(R.id.technician_dialog_close);
        technicianId = findViewById(R.id.technician_id);
        technicianName = findViewById(R.id.technician_name);
        technicianSex = findViewById(R.id.technician_sex);
        technicianAge = findViewById(R.id.technician_age);
        technicianHeight = findViewById(R.id.technician_height);
        technicianWeight = findViewById(R.id.technician_weight);
        technicianHome = findViewById(R.id.technician_home);
        itemType = findViewById(R.id.item_type);
        technicianMemo = findViewById(R.id.technician_memo);
        technicianIamge = findViewById(R.id.technician_iamge);
        callWaiterTips = findViewById(R.id.call_waiter_tips);
        callWaiter = findViewById(R.id.call_waiter);
    }

    private void setDialogColse(){
        technicianDialogClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    private void setJSDialogData(){
        technicianId.setText(preJSDataBean.getNo());
        technicianName.setText(preJSDataBean.getName());
        technicianAge.setText(preJSDataBean.getAge() + "");
        technicianHeight.setText(preJSDataBean.getHeight());
        technicianWeight.setText(preJSDataBean.getWeight());
        technicianHome.setText(preJSDataBean.getLocal());
        technicianMemo.setText(preJSDataBean.getNotes());
        itemType.setText(itemName);
        if(preJSDataBean.getSex() == 0){
            technicianSex.setText("女");
        }else{
            technicianSex.setText("男");
        }
        String imageUrl = MyApplication.getHostResUrl() + "/upload/" + preJSDataBean.getImg();
        ImageUtil.displayImage_Round(context,imageUrl,technicianIamge,rod);
    }

    public void setPreJSDataBean(PreJSDataBean preJSDataBean) {
        this.preJSDataBean = preJSDataBean;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    public void setShowCall(boolean showCall) {
        isShowCall = showCall;
    }

}
